<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>登录 - ECharts配置管理系统</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.10.3/font/bootstrap-icons.css">
    <link rel="stylesheet" href="css/styles.css">
    <style>
        body {
            background-color: #f8f9fa;
        }
        .login-container {
            max-width: 400px;
            margin: 100px auto;
        }
        .card {
            border: none;
            box-shadow: 0 0.5rem 1rem rgba(0, 0, 0, 0.15);
        }
        .card-header {
            background-color: #fff;
            border-bottom: none;
            padding-bottom: 0;
        }
        .system-name {
            font-size: 1.5rem;
            font-weight: 600;
            color: #0d6efd;
            text-align: center;
            margin-bottom: 1rem;
        }
    </style>
</head>
<body>
    <div class="container login-container">
        <div class="card">
            <div class="card-header text-center pt-4"></div>
                <div class="system-name">ECharts配置管理系统</div>
                <h4 class="mb-3">用户登录</h4>
            </div>
            <div class="card-body p-4">
                <form id="loginForm">
                    <div class="mb-3">
                        <label for="username" class="form-label">用户名</label>
                        <div class="input-group">
                            <span class="input-group-text"><i class="bi bi-person"></i></span>
                            <input type="text" class="form-control" id="username" name="username" placeholder="请输入用户名" required>
                        </div>
                    </div>
                    <div class="mb-4">
                        <label for="password" class="form-label">密码</label>
                        <div class="input-group">
                            <span class="input-group-text"><i class="bi bi-lock"></i></span>
                            <input type="password" class="form-control" id="password" name="password" placeholder="请输入密码" required>
                        </div>
                    </div>
                    <div class="alert alert-danger d-none" id="loginError"></div>
                    <div class="d-grid gap-2">
                        <button type="submit" class="btn btn-primary" id="loginBtn">
                            <span class="spinner-border spinner-border-sm d-none" id="loginSpinner" role="status" aria-hidden="true"></span>
                            登录
                        </button>
                    </div>
                </form>
            </div>
            <div class="card-footer bg-white text-center py-3">
                <p class="mb-0">还没有账号？ <a href="register.html" class="text-primary">立即注册</a></p>
            </div>
        </div>
    </div>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/js/bootstrap.bundle.min.js"></script>
    <script src="js/auth.js"></script>
    <script>
        document.addEventListener('DOMContentLoaded', function() {
            // 检查是否已登录
            const token = localStorage.getItem('token');
            if (token) {
                // 验证令牌
                fetch('/api/auth/me', {
                    headers: {
                        'Authorization': `Bearer ${token}`
                    }
                })
                .then(response => {
                    if (response.ok) {
                        window.location.href = 'index.html';
                    }
                })
                .catch(() => {
                    // 令牌无效，清除本地存储
                    localStorage.removeItem('token');
                    localStorage.removeItem('user');
                });
            }

            // 登录表单提交
            document.getElementById('loginForm').addEventListener('submit', function(e) {
                e.preventDefault();
                
                const username = document.getElementById('username').value.trim();
                const password = document.getElementById('password').value;
                const loginBtn = document.getElementById('loginBtn');
                const loginSpinner = document.getElementById('loginSpinner');
                const loginError = document.getElementById('loginError');
                
                // 显示加载状态
                loginBtn.disabled = true;
                loginSpinner.classList.remove('d-none');
                loginError.classList.add('d-none');
                
                // 调用登录函数
                login(username, password)
                    .then(result => {
                        if (result.success) {
                            window.location.href = 'index.html';
                        } else {
                            loginError.textContent = result.message || '登录失败，请检查用户名和密码';
                            loginError.classList.remove('d-none');
                        }
                    })
                    .finally(() => {
                        // 恢复按钮状态
                        loginBtn.disabled = false;
                        loginSpinner.classList.add('d-none');
                    });
            });
        });
    </script>
</body>
</html>